package com.syher.swagger.controller;

import com.google.common.collect.Lists;
import com.syher.swagger.swagger.SwaggerHttpMethodKeys;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.PostConstruct;
import java.io.Serializable;
import java.util.List;

/**
 * Created by shenyuhang on 2018/4/10.
 */
@RestController
@RequestMapping("/user")
@Api(description = "用户管理")
public class UserController {
    private static final Integer userSize = 20;
    private List<User> users = Lists.newArrayListWithExpectedSize(userSize);

    @PostConstruct
    public void instance() {
        for (int i = 0; i < userSize; i++) {
            User user = new User();
            user.setId(i);
            user.setName("name" + i);
            users.add(user);
        }
    }

    @RequestMapping("/list")
    @ApiOperation(value = "查询学生列表", httpMethod = SwaggerHttpMethodKeys.POST)
    public List<User> list() {
        return users;
    }

    @RequestMapping("/getUser")
    @ApiOperation(value = "根据id查询学生信息", httpMethod = SwaggerHttpMethodKeys.POST)
    public User getUsers(Integer id) {
        return users.stream().filter(user -> id == user.getId()).findFirst().get();
    }

    class User implements Serializable {
        private Integer id;
        private String name;

        public Integer getId() {
            return id;
        }

        public void setId(Integer id) {
            this.id = id;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }
    }
}
